package blue.mqtt4j.core.util;

import java.util.Random;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-30
 */
public class ClientIdGen
{
	public static final String PREFIX = "mqtt4j";
	private static final String chars = "0123456789abcdefghijklmnopqrxtuvwxyz";

	private static ClientIdGen instance = new ClientIdGen();

	private String clientId;

	private ClientIdGen()
	{
		String random = this.random(10);
		this.clientId = PREFIX + "-" + random;
	}

	public static ClientIdGen getInstance()
	{
		return instance;
	}

	public String clientId()
	{
		return clientId;
	}

	private String random(int length)
	{
		Random random = new Random();
		int count = chars.length();
		StringBuilder sb = new StringBuilder(length);
		for (int i = 0; i < length; i++)
		{
			int index = random.nextInt(count);
			sb.append(chars.charAt(index));
		}
		return sb.toString();
	}

}
